package com.xhj.joffer.stack;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
/**
 * @author haijianxia pure_xhj@163.com
 * @Date 2021-09-16
 * @Time 14:58
 */
public class TwoStackSimulateQueue {
  public static void main(String[] args) {
	  QueueByStack queueByStack = new QueueByStack();
	  int value=0;

    System.out.println("pop: "+queueByStack.pop());System.out.println(queueByStack.toString()+"\n");
	  value=1;queueByStack.push(value);System.out.println("push: "+value);System.out.println(queueByStack.toString()+"\n");
	  value=2;queueByStack.push(value);System.out.println("push: "+value);System.out.println(queueByStack.toString()+"\n");
	  value=3;queueByStack.push(value);System.out.println("push: "+value);System.out.println(queueByStack.toString()+"\n");

	  System.out.println("pop: "+queueByStack.pop());System.out.println(queueByStack.toString()+"\n");
	  System.out.println("pop: "+queueByStack.pop());System.out.println(queueByStack.toString()+"\n");
	  System.out.println("pop: "+queueByStack.pop());System.out.println(queueByStack.toString()+"\n");
	  System.out.println("pop: "+queueByStack.pop());System.out.println(queueByStack.toString()+"\n");

	  queueByStack.pushs(11,22,33,44,55,66);System.out.println(queueByStack.toString()+"\n");
	  System.out.println("pop: "+queueByStack.pop());System.out.println(queueByStack.toString()+"\n");
	  System.out.println("pop: "+queueByStack.pop());System.out.println(queueByStack.toString()+"\n");
	  System.out.println("pop: "+queueByStack.pop());System.out.println(queueByStack.toString()+"\n");
	  System.out.println("pop: "+queueByStack.pop());System.out.println(queueByStack.toString()+"\n");
	  System.out.println("pop: "+queueByStack.pop());System.out.println(queueByStack.toString()+"\n");
	  System.out.println("pop: "+queueByStack.pop());System.out.println(queueByStack.toString()+"\n");
	  System.out.println("pop: "+queueByStack.pop());System.out.println(queueByStack.toString()+"\n");

	  System.out.println("==========================大批量测试开始==========================");

	  System.out.println("\n=============test01==============");
	  System.out.println("");
	  long time01 = System.currentTimeMillis();
		
	  int pushTimes=1200, popTimes=3232;
		int pushTimesNow=0, popTimesNow=0;

    while(pushTimesNow<pushTimes || popTimesNow<popTimes) {
	    if(pushTimesNow<pushTimes){
		    Random random = new Random();
		    int nextInt = random.nextInt(pushTimes-pushTimesNow+1);
        for (int i1 = 0; i1 < nextInt; i1++,pushTimesNow++) {
	        queueByStack.pushs(i1);System.out.println(queueByStack.toString()+"\n");
        }
	    }
	    if(popTimesNow<popTimes){
		    Random random = new Random();
		    int nextInt = random.nextInt(popTimes-popTimesNow+1);
		    for (int i1 = 0; i1 < nextInt; i1++,popTimesNow++) {
			    System.out.println("pop: "+queueByStack.pop());System.out.println(queueByStack.toString()+"\n");
		    }
	    }
    }

	  long time0101  = System.currentTimeMillis() - time01;
	  System.out.println("test01 耗时 =  " + time0101  + "ms");
	  System.out.println("=================================");
	  

  }
  
  
	private static class QueueByStack{
		Stack dataStack =new Stack();
		Stack catchStack =new Stack();
		
  	public void push(int value){
  	  dataStack.push(value);
	  }
  	public void pushs(int ... values){
      for (int i = 0; i < values.length; i++) {
	      dataStack.push(values[i]);
      }
	  }

	  public int pop(){
			int value=-2;
		  int length = dataStack.stack.length;
		  for (int i = 0; i < length -1; i++) {
	      int pop = dataStack.pop();
	      catchStack.push(pop);
      }
		  try {
			  value= dataStack.pop();
		  } catch (Exception e) {
		  value=-1; } finally{}

      for (int i = 0; i < length-1; i++) {
	      int pop = catchStack.pop();
	      dataStack.push(pop);
      }
		  return value;
		}
		@Override
		public String toString() {
			return "data: "+Arrays.toString(dataStack.stack);
		}
	}
	
	private static class Stack{
		int[] stack;
		int[] stackSort;

		public Stack() {
			stack=new int[0];
			stackSort=new int[0];
		}
		public Stack(ArrayList<Integer> list) {
			stack=new int[list.size()];
			for (int i = 0; i < list.size(); i++) {
				push(list.get(i));
			}
		}
		public Stack(int... array) {
			for (int i = 0; i < array.length; i++) {
				push(array[i]);
			}
		}

		public void push(int value){
			if (stack == null) {
				stack= new int[]{value};
				stackSort= new int[]{value};
				return;
			}
			int lengthTemp = stack.length + 1;
			int[] stackTemp=new int[lengthTemp];
			for (int i = 0; i < stack.length; i++) {
				stackTemp[i]=stack[i];
			}
			stackTemp[lengthTemp-1]=value;

			// 小则插入
			int[] stackSortTemp=new int[lengthTemp];
			int indexTemp=0, times=1;
			for (int i = 0; i < lengthTemp-1; i++) {
				if (times==1 && value<stackSort[i]){
					stackSortTemp[indexTemp++]=value;times=0;
				}
				stackSortTemp[indexTemp++]=stackSort[i];
			}
			if (times == 1) {
				stackSortTemp[indexTemp++]=value;
			}

			// 更新
			stack=stackTemp;
			stackSort=stackSortTemp;
		}

		public int pop(){
			int value=0;
			if (stack==null && stack.length==0){
				throw new NullPointerException("当前栈为空");
			}

			int lengthTemp = stack.length - 1;

			// stack 弹出末尾元素
			int[] stackTemp=new int[lengthTemp];
			for (int i = 0; i < lengthTemp; i++) {
				stackTemp[i]=stack[i];
			}

			value=stack[stack.length -1];

			// 等则删除
			int[] stackSortTemp=new int[lengthTemp];
			int indexTemp=0, times=1;
			for (int i = 0; i < stack.length; i++) {
				if (times==1 && value==stackSort[i]){
					times=0;continue;
				}
				stackSortTemp[indexTemp++]=stackSort[i];
			}

			// 更新
			stack=stackTemp;
			stackSort=stackSortTemp;

			return value;
		}

		public void popAll() {
			int times=stack.length;
			for (int i = 0; i < times; i++) {
				pop();
			}
		}

		public int findMin(){
			if (stack.length==0){
				throw new NullPointerException("当前栈为空");
			}
			return stackSort[0];
		}

		@Override
		public String toString(){
			return "stack: "+ Arrays.toString(stack)+"\nstackSort: "+Arrays.toString(stackSort);
		}
	}

}
